package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.*;
import com.itheima.dao.OrdersDao;
import com.itheima.service.*;
import com.itheima.utils.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersDao , Orders> implements OrdersService {

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @Override
    public int submitOrder(Orders orders) {

        //0. 查询用户数据和地址数据
        User user = userService.getById(BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());

        //1. 补充数据
        //1.1 设置number 订单号
        orders.setNumber(UUID.randomUUID().toString().replace("-", ""));
        //1.2 设置status
        orders.setStatus(2);
        //1.3 设置user_id
        orders.setUserId(BaseContext.getCurrentId());
        //1.4 设置addressBookId::不用设置，页面传递过来
        //1.5 设置orderTime
        orders.setOrderTime(LocalDateTime.now());
        //1.6 设置checkOutTime
        orders.setCheckoutTime(LocalDateTime.now());
        //1.7 设置PayMethod
        orders.setPayMethod(1);
        //1.8 设置amount::订单的总价，页面传递上来，不需要设置
        //1.9 设置remark::，页面传递上来，不需要设置
        //1.10 设置phone
        orders.setPhone(user.getPhone());
        //1.11 设置address
        String address = (addressBook.getProvinceName() != null ? addressBook.getProvinceName() : "") +
                (addressBook.getCityName() != null ? addressBook.getCityName() : "") +
                (addressBook.getDistrictName() != null ? addressBook.getDistrictName() : "") +
                (addressBook.getDetail() != null ? addressBook.getDetail() : "") ;

        orders.setAddress(address);
        //1.12 设置user_name
        orders.setUserName(user.getName());
        //1.13 设置consignee
        orders.setConsignee(addressBook.getConsignee());


        //2. 先添加订单表
        boolean result1 = this.save(orders);

        //3. 再添加订单详情表

        //3.1 获取所有购物车的数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId , BaseContext.getCurrentId());
        List<ShoppingCart> cartList = shoppingCartService.list(lqw);

        //3.2 10个购物车商品，就要转化成濑10个详情对象
        List<OrderDetail> detailList = cartList.stream().map(shoppingCart -> {

            //1. 构建订单详情对象
            OrderDetail orderDetail = new OrderDetail();

            //2. 拷贝数据
            BeanUtils.copyProperties(shoppingCart, orderDetail);

            //3. 设置订单id
            orderDetail.setOrderId(orders.getId());


            return orderDetail;
        }).collect(Collectors.toList());

        //3.3 直接添加即可
        boolean result2 = orderDetailService.saveBatch(detailList);

        //添加成功了之后，要记得清空购物车数据。
        if(result1 && result2){

            //清空购物车
            shoppingCartService.remove(lqw);

            return 1;
        }

        return 0 ;
    }
}
